/*
 * Copyright 2007-2009 Perttu "celeron55" Ahola <celeron55@gmail.com>
 *
 * This file is part of jukuMagic.
 *
 * jukuMagic is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * jukuMagic is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with jukuMagic.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "commonfunctions.h"
#include <stdio.h>

//hieno on? no, ei sitä kovin usein käytetä...
string trim(string str)
{
	while(
			str.length()>0
			&&
			(
			 str.substr(0,               1)==" "     ||
			 str.substr(0,               1)=="\t"    ||
			 str.substr(0,               1)=="\r"    ||
			 str.substr(0,               1)=="\n"    ||
			 str.substr(str.length()-1,  1)==" "     ||
			 str.substr(str.length()-1,  1)=="\t"    ||
			 str.substr(str.length()-1,  1)=="\r"    ||
			 str.substr(str.length()-1,  1)=="\n"
			)
		 )
	{
		if      (str.substr(0,              1)==" ")    str = str.substr(1,str.length()-1);
		else if (str.substr(0,              1)=="\t")   str = str.substr(1,str.length()-1);
		else if (str.substr(0,              1)=="\r")   str = str.substr(1,str.length()-1);
		else if (str.substr(0,              1)=="\n")   str = str.substr(1,str.length()-1);
		else if (str.substr(str.length()-1, 1)==" ")    str = str.substr(0,str.length()-1);
		else if (str.substr(str.length()-1, 1)=="\t")   str = str.substr(0,str.length()-1);
		else if (str.substr(str.length()-1, 1)=="\r")   str = str.substr(0,str.length()-1);
		else if (str.substr(str.length()-1, 1)=="\n")   str = str.substr(0,str.length()-1);
	}
	return str;
}

string itos(int i){
	char ab[10];
	sprintf(ab, "%i", i);
	string keb=ab;
	return keb;
}

std::string dtos(double d, int p){
	char sammacko[100];
	std::string mite = "%."+itos(p)+"f";
	sprintf(sammacko, mite.c_str(), d);
	std::string hihhelis = sammacko;
	return hihhelis;
}


void strfnd::moveto(unsigned int place){
	p = place;
}
void strfnd::start(string niinq){
	tek = niinq;
	p=0;
}
string strfnd::next(string plop){
	size_t n;
	string palautus;
	if (p < tek.size())
	{
		if ((n = tek.find(plop, p)) == string::npos || plop == "")
		{
			n = tek.size();
		}
		else
		{
		}
		palautus = tek.substr(p, n-p);
		p = n + plop.length();
	}
	return palautus;
}
bool strfnd::inend(){
	if(p>=tek.size()) return true;
	return false;
}
strfnd::strfnd(string s){
	start(s);
}




